package no.hib.mod250.chat.client.util;

import java.util.ArrayList;
import java.util.List;

public class ObjectWithEvent<T> {

	public static interface Listener<T> {
		public void onChanged(T value);
	}

	private T object = null;
	private List<ObjectWithEvent.Listener<T>> listeners = new ArrayList<ObjectWithEvent.Listener<T>>();

	public ObjectWithEvent(T object) {
		this.object = object;
	}

	public void addListener(ObjectWithEvent.Listener<T> listner) {
		this.listeners.add(listner);
	}

	public void removeListner(ObjectWithEvent.Listener<T> listner) {
		this.listeners.remove(listner);
	}

	public T getObject() {
		return this.object;
	}

	public void setObject(T object) {
		this.object = object;
		notifyChange();
	}

	private void notifyChange() {
		for (ObjectWithEvent.Listener<T> listener : this.listeners) {
			listener.onChanged(this.object);
		}
	}

}
